<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索框 -->
    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入内容"
        v-model="info.query"
        class="input-with-select"
      >
        <el-button
          @click="search"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
      <el-button type="primary" @click="addUsers">添加用户</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="usersData" border style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column prop="username" label="姓名"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="role_name" label="角色"> </el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeState(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            @click="bj(scope.row)"
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            @click="del(scope.row.id)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
          <el-button type="warning" icon="el-icon-star-off" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加模态框 -->
    <el-dialog title="添加" :visible.sync="adddialogVisible" width="50%">
      <el-form ref="form" :model="addForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>

        <el-form-item label="电话">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="certain">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑模态框 -->
    <el-dialog title="编辑" :visible.sync="edidialogVisible" width="50%">
      <el-form ref="form" :model="addForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="addForm.username" disabled></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>

        <el-form-item label="电话">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="edidialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edicertain">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUsers,
  addUsers,
  ediUsers,
  delUsers,
  stateUsers,
} from "../utils.js/api";
export default {
  data() {
    return {
      adddialogVisible: false,
      edidialogVisible: false,
      info: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      addForm: {},
      usersData: [],
      currUser: 0,
    };
  },
  created() {
    this.getUsers();
  },
  methods: {
    // 渲染列表
    async getUsers() {
      const res = await getUsers(this.info);
      console.log(res);
      this.usersData = res.users;
    },
    // 搜索用户
    search() {
      // if()
      this.getUsers();
    },
    // 添加事件
    addUsers() {
      this.adddialogVisible = !this.adddialogVisible;
      this.addForm = {};
    },
    // 添加确定
    certain() {
      this.adddialogVisible = !this.adddialogVisible;
      this.$refs.form.validate(async (valid) => {
        if (!valid) return false;
        const res = await addUsers(this.addForm);
        // console.log(res);
        this.getUsers();
        this.addForm = {};
      });
    },
    // 编辑
    bj(row) {
      this.edidialogVisible = !this.edidialogVisible;
      console.log(row);
      this.currUser = row.id;
      this.addForm = JSON.parse(JSON.stringify(row));
      this.getUsers();
    },
    // 编辑确定
    edicertain() {
      this.edidialogVisible = !this.edidialogVisible;
      this.$refs.form.validate(async (valid) => {
        if (!valid) return false;
        const res = await ediUsers(this.currUser, this.addForm);
        this.getUsers();
        this.addForm = {};
      });
    },
    //   删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delUsers(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getUsers();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 状态
    async changeState(row) {
      const { id, mg_state: mgState } = row;
      await stateUsers(id, mgState);
      this.getUsers();
    },
  },
};
</script>


<style lang="scss" scoped>
.el-input {
  width: 400px;
}
</style>